package org.softmed.rest.generation.guice;

import org.softmed.rest.generation.DefaultRESTModuleBuilder;
import org.softmed.rest.generation.HandlerSuite;
import org.softmed.rest.generation.ModuleBuilderManager;
import org.softmed.rest.generation.RESTCreator;
import org.softmed.rest.generation.RESTModuleBuilder;
import org.softmed.rest.generation.ScafoldFacade;
import org.softmed.rest.generation.builders.ListRESTCreator;
import org.softmed.rest.generation.builders.NonNativeFieldCreator;
import org.softmed.rest.generation.builders.PrimitiveFieldsAndWrappersRESTCreator;
import org.softmed.rest.generation.builders.PrimitiveOnlyGetAllowedCreator;
import org.softmed.rest.generation.builders.StringFieldCreator;
import org.softmed.rest.generation.solution.DefaultRefactorProcessor;
import org.softmed.rest.generation.solution.RefactorProcessor;
import org.softmed.rest.generation.suites.HandlerSuiteManager;
import org.softmed.rest.generation.suites.JTActionHandlerSuite;
import org.softmed.rest.generation.uri.URIBuilder;
import org.softmed.rest.server.core.guice.NamedAnnotation;

import com.google.inject.Binder;
import com.google.inject.Module;

public class GenerationModule implements Module {

	private String[] roots;
	private String mainPath;
	String rootAppsDir = "apps";
	String defaultXMLSuiteName = "DefaultXMLSuiteName";
	String defaultModuleBuilder = "DefaultModuleBuilder";
	String databaseBackupDirectory = "backups";


	public GenerationModule(String mainPath, String[] roots,
			String defaultXMLSuiteName) {
		this.roots = roots;
		this.mainPath = mainPath;
		if (defaultXMLSuiteName != null)
			this.defaultXMLSuiteName = defaultXMLSuiteName;
	}

	@Override
	public void configure(Binder binder) {


		binder.bind(RESTCreator.class).annotatedWith(
				new NamedAnnotation("listCreator")).to(ListRESTCreator.class);

		binder.bind(RESTCreator.class).annotatedWith(
				new NamedAnnotation("primitiveCreator")).to(
				PrimitiveFieldsAndWrappersRESTCreator.class);

		binder.bind(RESTCreator.class).annotatedWith(
				new NamedAnnotation("restrictedPrimitiveCreator")).to(
				PrimitiveOnlyGetAllowedCreator.class);

		binder.bind(RESTCreator.class).annotatedWith(
				new NamedAnnotation("stringCreator")).to(
				StringFieldCreator.class);

		binder.bind(RESTCreator.class).annotatedWith(
				new NamedAnnotation("nonNativeCreator")).to(
				NonNativeFieldCreator.class);


		binder.bindConstant().annotatedWith(new NamedAnnotation("rootDir")).to(
				rootAppsDir);

		binder.bindConstant().annotatedWith(
				new NamedAnnotation("DatabaseBackupDirectory")).to(
				databaseBackupDirectory);

		binder.bindConstant().annotatedWith(
				new NamedAnnotation("DefaultXMLSuiteName")).to(
				defaultXMLSuiteName);

		binder.bind(RefactorProcessor.class).to(DefaultRefactorProcessor.class);

		binder.bind(HandlerSuite.class).annotatedWith(
				new NamedAnnotation("DefaultHandlerSuite")).to(
				JTActionHandlerSuite.class);

		binder.requestStaticInjection(HandlerSuiteManager.class);

		binder.bind(RESTModuleBuilder.class).annotatedWith(
				new NamedAnnotation("DefaultModuleBuilder")).to(
				DefaultRESTModuleBuilder.class);

		binder.requestStaticInjection(ModuleBuilderManager.class);

		binder.requestStaticInjection(ScafoldFacade.class);

		URIBuilder.setMainPath(mainPath);

		
	}

	public String getDefaultXMLSuiteName() {
		return defaultXMLSuiteName;
	}

	public void setDefaultXMLSuiteName(String defaultXMLSuiteName) {
		this.defaultXMLSuiteName = defaultXMLSuiteName;
	}

	public String getRootAppsDir() {
		return rootAppsDir;
	}

	public void setRootAppsDir(String rootAppsDir) {
		this.rootAppsDir = rootAppsDir;
	}

	public String getDefaultModuleBuilder() {
		return defaultModuleBuilder;
	}

	public void setDefaultModuleBuilder(String defaultModuleBuilder) {
		this.defaultModuleBuilder = defaultModuleBuilder;
	}

	public String getDatabaseBackupDirectory() {
		return databaseBackupDirectory;
	}

	public void setDatabaseBackupDirectory(String databaseBackupDirectory) {
		this.databaseBackupDirectory = databaseBackupDirectory;
	}

	// public boolean isAutoGenOnBoot() {
	// return autoGenOnBoot;
	// }
	//
	// public void setAutoGenOnBoot(boolean autoGenOnBoot) {
	// this.autoGenOnBoot = autoGenOnBoot;
	// }

}
